<?php 
defined('RUN_KATANA') or die('Restricted access');
class ProductAttribute extends Object
{
	public $Type;
	public $Title;
	public $TitleImport;
	public $Effect;
	public $EffectImage;
	public $Predefined;
	public $Display;
	public $DisplayList;
	public $DisplayAll;
	public $ValTypes;
	public $Active;
	public $Values;
	public $ValuesArray;
	public $ValuesLoaded;
	public $TaxId;
	public $TaxValue;
	
	public function ProductAttribute() 
	{ 
		self::setDataChanged(false);
		self::setValuesLoaded(false);
	}
	
	/* GET FUNCTIONS */
	public function getType() { return intval($this->Type); }
	public function getTitle() { return self::formatText($this->Title); }
	public function getTitleImport() { return self::formatText(strtolower($this->TitleImport)); }
	public function getEffect() { return intval($this->Effect); }
	public function getEffectImage() { return intval($this->EffectImage); }
	public function getPredefined() { return intval($this->Predefined); }
	public function getDisplay() { return intval($this->Display); }
	public function getDisplayList() { return intval($this->DisplayList); }
	public function getDisplayAll() { return intval($this->DisplayAll); }
	public function getValTypes() { return intval($this->ValTypes); }
	public function getActive() { return intval($this->Active); }
	public function getValue($id=0) 
	{
		if($id > 0)
		{
			self::loadValues();
			if(isset($this->Values[$id]))
			{ return $this->Values[$id]; }
		}
	}
	public function getValues() 
	{
		self::loadValues();
		return $this->Values; 
	}
	public function getValuesArray() 
	{ 
		self::loadValues();
		return $this->ValuesArray; 
	}
	public function getValuesLoaded() { return $this->ValuesLoaded; }
	public function getTaxId() { return intval($this->TaxId); }
	public function getTaxValue() { return $this->TaxValue; }
	
	/* SET FUNCTIONS */
	public function setType($val) { $this->Type=intval($val); }
	public function setTitle($val) { $this->Title=self::formatText($val); }
	public function setTitleImport($val) { $this->TitleImport=self::formatText(strtolower($val)); }
	public function setEffect($val) { $this->Effect=intval($val); }
	public function setEffectImage($val) { $this->EffectImage = intval($val); }
	public function setPredefined($val) { $this->Predefined=intval($val); }
	public function setDisplay($val) { $this->Display=intval($val); }
	public function setDisplayList($val) { $this->DisplayList=intval($val); }
	public function setDisplayAll($val) { $this->DisplayAll=intval($val); }
	public function setValTypes($val) { $this->ValTypes=intval($val); }
	public function setActive($val) { $this->Active=intval($val); }
	public function setValues($val) { $this->Values=$val; }
	public function setValuesArray($val) { $this->ValuesArray=$val; }
	public function setValuesLoaded($val) { $this->ValuesLoaded=$val; }
	public function setTaxId($val) { $this->TaxId=intval($val); }
	public function setTaxValue($val) { $this->TaxValue=$val; }
	
	public function loadValues() 
	{
		if((self::getId() > 0) && (self::getPredefined()) && (self::getValuesLoaded() == false))
		{
			$sql  = ' SELECT * FROM '.DB_PERFIX.'product_attribute_values WHERE attribute_id='.self::getId().' ORDER BY value_order, id ASC;';
			$result = mysql_query($sql); 
			if(mysql_num_rows($result) > 0)
			{
				while($row = mysql_fetch_array($result))
				{ 
					$this->Values[$row['id']] = new ProductAttributeValue($row['id'], self::getId(), $row['value'], $row['postfix'], $row['postfix_position'], $row['value_price'], $row['value_order'], $row['active'], $row['visible']);
					
					$this->Values[$row['id']]->setTaxId(self::getTaxId());
					$this->Values[$row['id']]->setTaxValue(self::getTaxValue());
					
					foreach(array_keys($row) as $key)
					{ $this->ValuesArray[$row['id']][$key] = $row[$key]; }
				}
			}
			self::setValuesLoaded(true);
		}
	}
	
	public function update()
	{
		if((self::getId() > 0) && (self::getDataChanged()))
		{
			$sql  = 'UPDATE '.DB_PERFIX.'product_attributes SET ';
			$sql .= '`type`='.self::getType().', ';
			if(self::getTitle() != '')
			{ $sql .= '`title`="'.self::formatText(self::getTitle()).'", '; }
			if(self::getTitleImport() != '')
			{ $sql .= '`title_import`="'.self::formatText(self::getTitleImport()).'", '; }
			$sql .= '`effect`='.self::getEffect().', ';
			$sql .= '`effect_image`='.self::getEffectImage().', ';
			$sql .= '`predefined`='.self::getPredefined().', ';
			$sql .= '`display`='.self::getDisplay().', ';
			$sql .= '`display_list`='.self::getDisplayList().', ';
			$sql .= '`display_all`='.self::getDisplayAll().', ';
			$sql .= '`val_types`='.self::getValTypes().', ';
			$sql .= '`active`='.self::getActive().' ';
			$sql .= ' WHERE `id`='.self::getId().';';
			$result = mysql_query($sql);
		}
	}
}
?>